package renderers
{
	import com.salesforce.results.QueryResult;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Label;
	import mx.core.FlexGlobals;
	import mx.core.IFactory;
	import mx.managers.PopUpManager;
	import components.GridDisplay;
	
	public class QueryResultRenderer extends Label implements IFactory
	{
		public function newInstance():*
		{
			return new QueryResultRenderer();
		} 

		private var qr:QueryResult;
		private var objName:String;
		
		// setting data and design
		override public function set data(value:Object):void
		{
			if (value!=null)
			{
				for (var key:String in value) {
					if (value[key] is QueryResult) {
						qr = value[key] as QueryResult;
						objName = key;
						var fieldDef:QueryResult = value[key] as QueryResult;
						if (qr.size == 0) {
							text = key + "(empty)";
						} else {
							text = key + "(" + qr.records.length + ")";
							this.useHandCursor = true;
							this.mouseChildren = false;
							this.buttonMode = true;
							this.setStyle("textDecoration", "underline");
							addEventListener(MouseEvent.CLICK, showChildren);
						}
					}
				}
			}
			super.data=value;
		}

		private function showChildren(evt:MouseEvent):void {
			var win:GridDisplay = PopUpManager.createPopUp(this, GridDisplay, true) as GridDisplay;
			//PopUpManager.centerPopUp(win);
			win.dataValue = qr;
			win.title = objName;
			win.x = stage.x + (stage.nativeWindow.width / 2) - (win.width / 2);
			win.y = 25;
		}
	}
}
